<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>注册</title>
  <!-- 导入bootstrap -->
  <script src="./assets/lib/jquery-3.6.0.min.js"></script>
  <link rel="stylesheet" href="./assets/lib/bootstrap/bootstrap.min.css" />
  <script src="./assets/lib/bootstrap/bootstrap.min.js"></script>
  <!-- bootstrap提示框 -->
  <script src="./assets/lib/toasts.js"></script>
  <!-- 导入字体图标 -->
  <link rel="stylesheet" href="./assets/iconfont/iconfont.css" />
  <!-- 导入axios + axios全局配置 -->
  <script src="./assets/lib/axios.js"></script>
  <script src="./assets/utils/request.js"></script>
  <!-- 导入自己写的css -->
  <link rel="stylesheet" href="./assets/css/login.css" />


</head>

<body>
  <div class="area">
    <div class="top">
      <img src="./assets/images/logo2.png" class="col-6" alt="" />
      <h1>学员管理系统</h1>
    </div>
    <div class="row">
      <div class="left">
        <img src="./assets/images/slogan.png" alt="" />
      </div>
      <div class="form-box">
        <div class="register box">
          <div class="container row header mb-4">
            <h2>欢迎注册</h2>
          </div>
          <form action="" class="container">
            <!-- 账号 -->
            <div class="row mb-4 form-group">
              <span class="iconfont icon-yonghu"></span>
              <input type="text" class="form-control" name="username" placeholder="请输入账号" />

            </div>
            <!-- 密码 -->
            <div class="row mb-4 form-group">
              <span class="iconfont icon-mima"></span>
              <input type="password" class="form-control" name="password" placeholder="请输入密码" />
            </div>
            <div class="row mb-4">
              <button type="submit" class="btn btn-primary">注册</button>
            </div>
            <div class="row mb-3">
              <a href="javascript:;" class="goLogin">已有账号，去登录</a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
  <!-- toast -->
  <div class="position-fixed top-0 start-50 pt-4" style="z-index: 999; transform: translateX(-50%)">
    <div id="myToast" class="toast bg-rgba">
      <div class="toast-body">提示消息</div>
    </div>
  </div>

  <!-- 页面js代码 -->
  <script src="./assets/js/login.js"></script>
  <script>
    //(1)收集表单数据
    //(2)校验表单数据（非空+正则）
    //(3)发送ajax请求（根据接口文档）
    //(4)服务器响应（保存token,跳转首页）
    const form = document.querySelector('.form-box')
    // const toast = new bootstrap.Toast('#myToast')
    document.querySelector('.goLogin').addEventListener('click', () => {
      location.href = './login.html'
    })

    const usernameR = document.querySelector('[name=username]')
    const passwordR = document.querySelector('[name=password]')
    //1.点击注册时收集表单数据
    document.querySelector('.btn-primary').addEventListener('click', async (e) => {
      e.preventDefault()
      //console.log(usernameR.value);
      const username = usernameR.value
      const password = passwordR.value

      //(2)校验表单数据（非空+正则）
      if (!username || !password) {
        Toast.fail('输入账号或者密码不能为空')
        return
      }
      if (username.length < 2 || username.length > 30) {
        Toast.fail('账号长度在2-30位之间')
        return
      }
      if (password.length < 6 || password.length > 30) {
        Toast.fail('密码长度在6-30位之间')
        return
      }

      //(3)发送ajax请求（根据接口文档）
      const res = await axios({
        url: 'http://www.itcbc.com:8000/api/register',
        method: 'post',
        data: {
          username,
          password
        }
      })

      if (res.data.code === 0) {
        console.log(res)//data.message
        Toast.success(res.data.message)
        setTimeout(() =>
          location.href = './login.html', 2000)
      }
      if (res.data.code === 1) {
        console.log(res)
        Toast.info(res.data.message)
      }
    })

  </script>
</body>

</html>